#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install lnmp"
    exit 1
fi

cur_dir=$(pwd)
Stack=$1

LNMP_Ver='1.3'

. lnmp.conf
. include/main.sh

shopt -s extglob

Check_DB
Get_OS_Bit
Get_Dist_Name

clear
echo "+------------------------------------------------------------------------+"
echo "|          LNMP V${LNMP_Ver} for ${DISTRO} Linux Server, Written by Licess          |"
echo "+------------------------------------------------------------------------+"
echo "|        A tool to auto-compile & install Nginx+MySQL+PHP on Linux       |"
echo "+------------------------------------------------------------------------+"
echo "|          For more information please visit http://www.lnmp.org         |"
echo "+------------------------------------------------------------------------+"


Uninstall_MySQL()
{
    echo "Stoping MySQL..."

    /etc/init.d/${DB_Name} stop

    Remove_StartUp ${DB_Name}
    if [ -d "${MariaDB_Data_Dir}" ]; then
        mv ${MariaDB_Data_Dir} /root/databases_backup_$(date +"%Y%m%d%H%M%S")
    else
        mv ${MySQL_Data_Dir} /root/databases_backup_$(date +"%Y%m%d%H%M%S")
    fi
    echo "Deleting MySQL files..."
    rm -rf /usr/local/${DB_Name}

    rm -f /etc/my.cnf
    rm -f /etc/init.d/${DB_Name}
    echo "MySQL Uninstall completed."
}

Uninstall_Memcached()
{
    echo "You will uninstall Memcached..."
    Press_Start
    /etc/init.d/memcached stop
    Remove_StartUp memcached
    echo "Delete Memcached files..."
    rm -rf /usr/local/libmemcached
    rm -rf /usr/local/memcached
    rm -rf /etc/init.d/memcached
    rm -rf /usr/bin/memcached
    if [ -s /sbin/iptables ]; then
        /sbin/iptables -D INPUT -p tcp --dport 11211 -j DROP
        /sbin/iptables -D INPUT -p udp --dport 11211 -j DROP
        if [ "$PM" = "yum" ]; then
            service iptables save
        elif [ "$PM" = "apt" ]; then
            iptables-save > /etc/iptables.rules
        fi
    fi
    echo "Uninstall Memcached completed."
}

Uninstall_Apache()
{
    echo "Stoping Apache..."

    /etc/init.d/httpd stop
    Remove_StartUp httpd

    echo "Deleting Apache files..."
    rm -rf /usr/local/apache
    rm -rf /usr/local/php
    rm -rf /usr/local/zend

    rm -f /etc/init.d/httpd
    echo "apache Uninstall completed."
}

Uninstall_Nginx()
{
    echo "Stoping Nginx..."

    /etc/init.d/nginx stop
    Remove_StartUp nginx

    echo "Deleting Nginx files..."
    rm -rf /usr/local/nginx
    rm -f /etc/init.d/nginx

    echo "Nginx Uninstall completed."
}


    Check_Stack
    echo "Current Stack: ${Get_Stack}"

    action=""
    echo "Enter 1 to uninstall MySQL(Only)"
    echo "Enter 2 to uninstall Memcached(Only)"
    echo "Enter 3 to uninstall Apache(LAMP)"
    echo "Enter 3 to uninstall Nginx(Only)"
    read -p "(Please input 1, 2 or 3):" action

    case "$action" in
    1|[mM][yY][sS][qQ][lL])
        echo "You will uninstall MySQL"
        Echo_Red "Please backup your configure files and mysql data!!!!!!"
        Echo_Red "The following directory or files will be remove!"
        cat << EOF
${MySQL_Dir}
/etc/init.d/${DB_Name}
/etc/my.cnf
EOF
        sleep 3
        Press_Start
        Uninstall_MySQL
    ;;
    2|[mM][eE][mM][cC][aA][cC][hH][eE])
        echo "You will uninstall Memcache"
        cat << EOF
/usr/local/nginx
${MySQL_Dir}
/usr/local/libmemcached
/usr/local/memcached
/etc/init.d/memcached
/usr/bin/memcached
EOF
        sleep 3
        Press_Start
        Uninstall_Memcached
    ;;
    3|[aA][pP][aA][cC][hH][eE])
        echo "You will uninstall Apache"
        cat << EOF
/usr/local/apache
/etc/init.d/httpd
/usr/local/php
/usr/local/zend
EOF
        sleep 3
        Press_Start
        Uninstall_Apache
    ;;
    4|[nN][gG][iI][nN][xX])
        echo "You will uninstall Nginx"
        cat << EOF
/usr/local/nginx
/etc/init.d/nginx
EOF
        sleep 3
        Press_Start
        Uninstall_Nginx
    
    ;;
    esac

